#ifndef _MYREDIS_H_
#define _MYREDIS_H_
#include <string>
#include <hiredis/hiredis.h>

using std::string;

class MyRedis {
public:
    static MyRedis* getInstance();
    static void init();
    static void destroy();

    void init(redisContext* rc, redisReply* rr);

    //connect redis
    bool connect(const string &ip, int port);

    //string
    void set(string key, string value);
    string get(string key);
    void select(int idx);
    //hash
    void hset(string table, string key, string value);   
    string hget(string table, string key);

    //list
    void lpush(string key, string value);
    void rpush(string key, string value);
    void lpop(string key);
    void rpop(string key);
    void lset(string key, size_t idx, string value);
    string lindex(string key, size_t idx);

    void multi();//开始事务
    void exec();//执行事务
    void discard();//取消事务

private:
    MyRedis()
    : _pConnect(nullptr)
    , _pReply(nullptr) { 
        connect("127.0.0.1", 6379); 
    }
    ~MyRedis() {}

private:
    static MyRedis* _pInstance;
    static pthread_once_t _once;
    redisContext* _pConnect;
    redisReply* _pReply;  
};

#endif